package org.social.comparable;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.bean.PlayerInfo;
import org.social.db.bean.PlayerMap;
import org.social.db.cache.PlayerMapCache;
import org.social.db.proxy.bean.PlayerMapProxy;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;

/**
 * @author : DengYing
 * @CreateDate : 2017年11月28日 下午6:58:31
 * @Description ：全服玩家等级排序器
 */
public class PlayerLevelComparable {
	
	static final Log log = LogFactory.get(PlayerLevelComparable.class);
	/**
	 * 排序桶 Integer:等级 Boolean:是否在线 Set：Pids
	 */
	private static Map<Integer,Map<Boolean,Set<Integer>>> levelBarrel = Maps.newConcurrentMap();
	/** 记录 */
	private static Map<Integer, String> record  = Maps.newConcurrentMap();
	// 最大等级
	private static int maxLvl = Integer.MIN_VALUE;
	// 最小等级
	private static int minLvl = Integer.MAX_VALUE;
	
	
	
	public static void putCachne(PlayerMap playerMap){
		PlayerMapProxy proxy = playerMap.getPlayerMapProxy();
		String content = record.remove(playerMap.getPid());
		Map<Boolean, Set<Integer>> statusMap = null;
		Set<Integer> pidSet = null;
		int lvl = 0;
		boolean online = false;
		if (content != null) {
			String[] contents = content.split("-");
			lvl = Integer.valueOf(contents[0]);
			online = Boolean.valueOf(contents[1]);
			
			// 当前等级桶状态集
			statusMap = levelBarrel.get(lvl);
			if (statusMap == null) {
				statusMap = Maps.newHashMapWithExpectedSize(2);
				levelBarrel.put(lvl, statusMap);
			}
			
			// 玩家集
			pidSet = statusMap.get(online);
			if (pidSet == null) {
				pidSet = Sets.newHashSetWithExpectedSize(128);
				statusMap.put(online, pidSet);
			}
			
			pidSet.remove((Integer)playerMap.getPid());
		}
		
		record.put(playerMap.getPid(), proxy.getPlv()+"-"+ proxy.isOnline());
		lvl = proxy.getPlv();
		online = proxy.isOnline();
		// 当前等级桶状态集
		statusMap = levelBarrel.get(lvl);
		if (statusMap == null) {
			statusMap = Maps.newHashMapWithExpectedSize(2);
			levelBarrel.put(lvl, statusMap);
		}
		
		// 玩家集
		pidSet = statusMap.get(online);
		if (pidSet == null) {
			pidSet = Sets.newHashSetWithExpectedSize(128);
			statusMap.put(online, pidSet);
		}
		pidSet.add(playerMap.getPid());
		
		maxLvl = Math.max(maxLvl, proxy.getPlv());
		minLvl = Math.min(minLvl, proxy.getPlv());
	}
	
	
	
	/** 取得记录信息 */
	public static String getRecordByPid(int pid){
		return record.get(pid);
	}

	public static int getMaxLvl() {
		return maxLvl;
	}

	public static int getMinLvl() {
		return minLvl;
	}
	
	
	public static Set<Integer> getPids(int lvl,boolean online,List<Integer> filterPids){
		Map<Boolean, Set<Integer>> statusMap = levelBarrel.get(lvl);
		if (statusMap == null || statusMap.isEmpty()) {
			return new HashSet<>(0);
		}
		Set<Integer> setPid = statusMap.get(online);
		if (setPid == null || setPid.isEmpty()) {
			return new HashSet<>(0);
		}
		return setPid.stream().filter(s -> !filterPids.contains(s)).collect(Collectors.toSet());	
	}
	
	public static void loadAll(){
		List<PlayerMap> playerMaps = PlayerMapCache.me().loadAll();
		for (PlayerMap playerMap : playerMaps) {
			PlayerLevelComparable.putCachne(playerMap);	
		}
	}
}
